package com.ziq.base.utils;

import android.content.Context;

import com.tencent.mars.xlog.Log;
import com.tencent.mars.xlog.Xlog;

/**
 * @author john.
 * @since 2018/5/25.
 * Des:
 */

public class LogUtil {
    private static final String TAG = "BaseLib";

    public static void init(Context context, boolean isDebug) {
        try {
            System.loadLibrary("c++_shared");
            System.loadLibrary("marsxlog");

            final String logPath = context.getFilesDir().getAbsolutePath() + "/xlog";
            // this is necessary, or may cash for SIGBUS
            final String cachePath = context.getFilesDir().getAbsolutePath() + "/xlog" + "/cache";
            final String namePrefix = "xLog_";

            //init xlog
            Xlog xlog = new Xlog();
            Log.setLogImp(xlog);

            if (isDebug) {
                Log.setConsoleLogOpen(true);
                Log.appenderOpen(Xlog.LEVEL_DEBUG, Xlog.AppednerModeAsync, cachePath, logPath, namePrefix, 0);
            } else {
                Log.setConsoleLogOpen(false);
                Log.appenderOpen(Xlog.LEVEL_INFO, Xlog.AppednerModeAsync, cachePath, logPath, namePrefix, 0);
            }

        } catch (Error | Exception e){
            e.printStackTrace();
        }
    }

    public static void flush() {
        Log.appenderFlush();
    }


    public static void e(String msg) {
        LogUtil.i(TAG, msg);
    }

    public static void e(String tag, String msg) {
        LogUtil.i(tag, msg);
    }

    public static void i(String msg) {
        LogUtil.i(TAG, msg);
    }

    public static void i(String tag, String msg) {
        Log.i(tag, msg);
    }

}
